package com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.commoncmps;

import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.AssoInfoBase;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import com.inspur.edp.dxc.common.domain.CommonResult;
import com.inspur.edp.dxc.entity.data.DxcCommonData;
import com.inspur.edp.dxc.spi.components.BaseAfterDataConvertComponent;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.sysmanager.api.data.org.GSPSysOrg;
import io.iec.edp.caf.sysmanager.api.data.user.User;
import io.iec.edp.caf.sysmanager.api.data.user.UserFilter;
import io.iec.edp.caf.sysmanager.api.data.user.UserPageData;
import io.iec.edp.caf.sysmanager.api.manager.org.SysOrgManager;
import io.iec.edp.caf.sysmanager.api.manager.user.UserManager;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DzyhpDrConver extends BaseAfterDataConvertComponent {

    String userConfig = "GspUser";

    String sysOrgConfig = "com.inspur.gs.gsp.sys.sysorg.SysOrg";


    public void dataConver() {
        List<ModifyChangeDetail> details = super.getDetails();

        IStandardLcp usrLcp = LcpUtil.getLcp(userConfig);
        IStandardLcp orgLcp = LcpUtil.getLcp(sysOrgConfig);

        for (ModifyChangeDetail detail : details) {
            setUserConfig(detail, usrLcp);
            setOrgConfig(detail, orgLcp);
        }

        CommonResult result = super.getResult();
        result.setChangeDetails(details);
    }

    private void setUserConfig(ModifyChangeDetail changeDetail, IStandardLcp lcp) {
        AssoInfoBase info = (AssoInfoBase) changeDetail.getPropertyChanges().get("syryid");
        if (info == null) {
            return;
        }
        String id = (String) info.getValue("syryid");

        UserManager userManager = SpringBeanUtils.getBean(UserManager.class);
        UserFilter userFilter = new UserFilter();
        userFilter.setCode(id);

        UserPageData usersByFilter = userManager.getUsersByFilter(userFilter);
        List<User> data = usersByFilter.getData();

        if (data.isEmpty()) {
            return;
        }

        info.setValue("syryid", data.get(0).getId());
    }

    private void setOrgConfig(ModifyChangeDetail changeDetail, IStandardLcp lcp) {
        AssoInfoBase info = (AssoInfoBase) changeDetail.getPropertyChanges().get("gkbmid");
        if (info == null) {
            return;
        }
        String id = (String) info.getValue("gkbmid");

        SysOrgManager sysOrgManager = SpringBeanUtils.getBean(SysOrgManager.class);

        List<GSPSysOrg> allOrgs = sysOrgManager.getAllOrgs();
        List<GSPSysOrg> data = allOrgs.stream().filter(i -> i.getName().equals(id)).collect(Collectors.toList());

        info.setValue("gkbmid", data.get(0).getId());
    }

}